package isp

import (
    `context`
    `errors`
)

// 03_repeated_inputs 重复输入

func performEncryption(key []byte, data []byte) []byte {
    // TODO: implement
    return nil
}

func Encrypt(ctx context.Context, data []byte) ([]byte, error) {
    // As this operation make take too long, we need to be able to kill it
    // 因为这个操作需要花费太长时间，我们需要能够杀死它。

    stop := ctx.Done()
    result := make(chan []byte, 1)

    go func() {
        defer close(result)

        // pull the encryption key from context
        key := ctx.Value("encryption-key").([]byte)

        // perform encryption
        ciperText := performEncryption(key, data)

        // signal complete by sending the result 通过发送结果完成信号
        result <- ciperText

    }()

    select {
    case ciperText := <-result:
        // happy path
        return ciperText, nil
    case <-stop:
        // cancelled
        return nil, errors.New("operation cancelled")



    }

}
